package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zhentao.common.constants.ArticleConstants;
import com.zhentao.mapper.ArticleMapper;
import com.zhentao.file.service.FileStorageService;
import com.zhentao.model.article.pojo.ApArticle;
import com.zhentao.model.es.vo.SearchArticleVo;
import com.zhentao.service.ArticleFreemarkerService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {

    @Autowired
    private Configuration configuration;
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private ArticleMapper articleMapper;

    @SneakyThrows
    @Override
    @Async  //异步调用
    public void buildArticleToMinIO(ApArticle apArticle, String content) {

        //1.判断内容是否存在
        if(StringUtils.isNotBlank(content)){
            //2.通过freemarker把内容生成html文件
            //2.1获取template模板
           Template template= configuration.getTemplate("article.ftl");
            //2.2数据模型
            Map<String,Object> contentDataMap=new HashMap<>();
            contentDataMap.put("content", JSONArray.parseArray(content));
            //2.3合成
            StringWriter out=new StringWriter();
            template.process(contentDataMap,out);

            //3.把html文件上传到minio里面
            InputStream in=new ByteArrayInputStream(out.toString().getBytes(StandardCharsets.UTF_8));
            String path=fileStorageService.uploadHtmlFile("articel",apArticle.getId()+".html",in);

            //4.修改ap_article表，保存static_url字段
            apArticle.setStaticUrl(path);
            articleMapper.updateById(apArticle);

            //发送消息，创建索引
            createArticleESIndex(apArticle,content,path);
        }
    }
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 送消息，创建索引
     * @param apArticle
     * @param content
     * @param path
     */
    private void createArticleESIndex(ApArticle apArticle, String content, String path) {
        SearchArticleVo vo = new SearchArticleVo();
        BeanUtils.copyProperties(apArticle,vo);
        vo.setContent(content);
        vo.setStaticUrl(path);

        kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(vo));
    }
}
